package cn.ainit.fx.jh;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mysql.jdbc.StringUtils;

import cn.ainit.common.ReturnObject;
import cn.ainit.fx.jh.entity.FxJh;
import cn.ainit.fx.jh.entity.FxJhItem;
import cn.ainit.fx.jh.entity.JhObj;
import cn.ainit.fx.pixiao.entity.FxPiXiao;
import cn.ainit.sys.utils.UUID32;

@Controller
@RequestMapping("/jh")
public class JhControllor {
	JhService jhService;
	
	@Resource
	public void setJhService(JhService jhService) {
		this.jhService = jhService;
	}
	
	@RequestMapping("/saveJh")
	@ResponseBody
	public ReturnObject savejh(@RequestBody JhObj jhObj, HttpServletRequest request){
		ReturnObject returnObject = new ReturnObject();
		FxJh fxJh = jhObj.getFxJh();
		List<FxJhItem> fxJhItems = jhObj.getFxJhItems();
		try {
			if (fxJh.getJhId() != null && !fxJh.getJhId().equals("")) {
				//删除旧的不存在的记录    条件  sp_id 和 sp_item_id
				if(jhService.updateFxJh(fxJh,fxJhItems)){
					returnObject.setMsg("upsuccess");
				}else{
					returnObject.setMsg("upfail");
				}
			}else{
				String jhId = UUID32.newGuid();
				fxJh.setJhId(jhId);
				//保存操作
				if(jhService.saveFxJh(fxJh,fxJhItems)){
					returnObject.setMsg("success");
					returnObject.setInfo(jhId);
				}else{
					returnObject.setMsg("fail");
				}
			}
		} catch (Exception e) {
			returnObject.setMsg(e.getMessage());
			e.printStackTrace();
		}
		return returnObject;
	}
	/**
	 * 进货单  退货单 入库出库
	 * @param jhId
	 * @param stockMan
	 * @param request
	 * @return
	 * @throws Exception
	 * @author wyang
	 * @date : 2014-4-17 上午11:19:01
	 */
	@RequestMapping("/jhRuku")
	@ResponseBody
	public ReturnObject jhRuku(String jhId,String stockMan,HttpServletRequest request) throws Exception{
		ReturnObject returnObject = new ReturnObject();
		try {
			String msg = this.jhService.execJhRuku(jhId, "");
			returnObject.setMsg(msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnObject;
	}
	/**
	 * 进货单 退货单的承付
	 * @param jhId
	 * @param finMan
	 * @return
	 * @throws Exception
	 * @author wyang
	 * @date : 2014-4-22 上午10:09:34
	 */
	@RequestMapping("/chengfu")
	@ResponseBody
	public ReturnObject chengfu(String jhId,String finMan) throws Exception{
		ReturnObject returnObject = new ReturnObject();
		try {
			String msg = this.jhService.execChengfu(jhId, finMan);
			returnObject.setMsg(msg);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnObject;
	}
	/**
	 * 根据供货商Id 查询到货退货的记录
	 * @param manId
	 * @param request
	 * @return
	 * @throws Exception
	 * @author wyang
	 * @date : 2014-4-26 下午03:30:33
	 */ 
	@RequestMapping("/shouPiaoItems")  
	@ResponseBody
	public ReturnObject shouPiaoItems(String manId,HttpServletRequest request) throws Exception{
		ReturnObject returnObject = new ReturnObject();
		try {
			List<FxJh> list = this.jhService.daoHuoByMan(manId);
			returnObject.setObjList(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnObject ;
	}
}
